package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/setmeal")
@Slf4j
@ApiOperation("套餐功能")
public class SetmealController {

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result save(@RequestBody  SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealService.save(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if(list!=null && list.size()>0){
           list.forEach(li->{
               li.setSetmealId(id);
           });
           setmealDishMapper.batchinsert(list);
        }
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        Long id = setmealDTO.getId();
        if(list!=null && list.size()>0){
            list.forEach(li->{
                li.setSetmealId(id);
            });
            setmealDishMapper.batchinsert(list);
        }
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查")
    @Cacheable(cacheNames = "setmealCache" ,key = "#{id}")
    public Result<SetmealVO> querybyid(@PathVariable Long id){
       SetmealVO setmealVO =  setmealService.querybyid(id);
       List<SetmealDish> setmealDish = setmealDishMapper.query(id);
       setmealVO.setSetmealDishes(setmealDish);
        return  Result.success(setmealVO);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("设置套餐启售停售状态")
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result startstatus(@PathVariable Integer status,Long id){
        SetmealDTO setmealDTO =  SetmealDTO.builder()
                .id(id)
                        .status(status).build();
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除")
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result deleteids(@RequestParam List<Long> ids){
        setmealService.deleteids(ids);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> querypage(SetmealPageQueryDTO setmealPageQueryDTO){
       PageResult pageResult =  setmealService.querypage(setmealPageQueryDTO);
       return Result.success(pageResult);
    }



}
